package com.yuntsg.nnsfcp.test.pdf;

import com.aspose.pdf.*;

import java.util.List;

/**
 * @Author: 葛成瑞
 * @Date: 2024/9/23 14:59
 */
public class AsposedPdf {
    public static void main(String[] args) {
        // 测试aspose pdf 测试

        String pdfPath = "D:/Users/55419/Desktop/杂乱/国自然原始文件.pdf";
        String pdfPathWord = "D:/Users/55419/Desktop/杂乱/国自然原始文件222.docx";
        Document pdfDocument = new Document(pdfPath);
        PageCollection pages = pdfDocument.getPages();
        TableAbsorber absorber = new TableAbsorber();
        for (Page page : pages) {
            absorber.visit(page);
        }
        List<AbsorbedTable> tableList = absorber.getTableList();
        System.out.println("表格数量+" + tableList.size());
        for (int i = 0; i < tableList.size(); i++) {
            System.out.println("第" + (i + 1) + "个表格数据");
            AbsorbedTable absorbedTable = tableList.get(i);
            List<AbsorbedRow> rowList = absorbedTable.getRowList();
            Rectangle rectangle = absorbedTable.getRectangle();
            String string = rectangle.toString();
            System.out.println(string);
            System.out.println("这个表格行数+" + rowList.size());
            for (AbsorbedRow absorbedRow : rowList) {
                List<AbsorbedCell> cellList = absorbedRow.getCellList();
                System.out.println("++++++++++++++++当前行的列数+" + cellList.size());
                for (AbsorbedCell absorbedCell : cellList) {
                    TextFragmentCollection textFragments = absorbedCell.getTextFragments();
                    if (textFragments != null) {
                        //正常应该是一个 多个进行合并
                        StringBuffer stringBuffer = new StringBuffer();
                        if (textFragments.size() > 1) {
                            for (TextFragment textFragment : textFragments) {
                                stringBuffer.append(textFragment.getText().replace("\n", "").replace("\\u00A0", ""));
                            }
                            System.out.println(stringBuffer.toString());
                        } else {
                            for (TextFragment textFragment : textFragments) {
                                String text = textFragment.getText().replace("\n", "").replace("\\u00A0", "");
                                System.out.println(text);
                            }
                        }
                    }
                }
                System.out.println("+++++++++++++++rowend+++++++++++++++++++");
            }
            System.out.println("+++++++++++++++++++++++++++++++++++");
        }

        pdfDocument.save(pdfPathWord, SaveFormat.DocX);
    }
}
